<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Welcome to the Python GDAL/OGR Cookbook! &mdash; Python GDAL/OGR Cookbook 1.0 documentation</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Python GDAL/OGR Cookbook 1.0 documentation" href="#" />
    <link rel="next" title="GDAL/OGR General" href="gdal_general.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="gdal_general.html" title="GDAL/OGR General"
             accesskey="N">next</a> |</li>
        <li class="nav-item nav-item-0"><a href="#">Python GDAL/OGR Cookbook 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="welcome-to-the-python-gdal-ogr-cookbook">
<h1>Welcome to the Python GDAL/OGR Cookbook!<a class="headerlink" href="#welcome-to-the-python-gdal-ogr-cookbook" title="Permalink to this headline">¶</a></h1>
<p>This cookbook has simple code snippets on how to use the Python GDAL/OGR API.  The web site
is a project at <a class="reference external" href="https://github.com/pcjericks/py-gdalogr-cookbook">GitHub</a> and served by Github Pages.  If you find missing recipes or mistakes
in existing recipes please add an issue to the <a class="reference external" href="https://github.com/pcjericks/py-gdalogr-cookbook/issues?state=open">issue tracker</a>.</p>
<p>For a detailed description of the whole Python GDAL/OGR API, see the useful <a class="reference external" href="http://gdal.org/python/">API docs</a>.</p>
<p>We heavily relied on Chris Garrard&#8217;s excellent <a class="reference external" href="http://www.gis.usu.edu/~chrisg/python/2009/">Geoprocessing with Python using Open Source GIS</a>
and the official <a class="reference external" href="http://gdal.org/python/">GDAL/OGR Python documentation</a>. Another great source of examples is OGR&#8217;s <a class="reference external" href="https://svn.osgeo.org/gdal/trunk/autotest/ogr/">autotest directory</a>.</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="gdal_general.html">GDAL/OGR General</a><ul>
<li class="toctree-l2"><a class="reference internal" href="gdal_general.html#is-gdal-ogr-installed">Is GDAL/OGR Installed</a></li>
<li class="toctree-l2"><a class="reference internal" href="gdal_general.html#check-version-of-gdal-ogr-installed">Check Version of GDAL/OGR installed</a></li>
<li class="toctree-l2"><a class="reference internal" href="gdal_general.html#enable-python-exceptions">Enable python exceptions</a></li>
<li class="toctree-l2"><a class="reference internal" href="gdal_general.html#install-gdal-ogr-error-handler">Install GDAL/OGR error handler</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="geometry.html">Geometry</a><ul>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-a-point">Create a Point</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-a-linestring">Create a LineString</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-a-polygon">Create a Polygon</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-a-polygon-with-holes">Create a Polygon with holes</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-a-multipoint">Create a MultiPoint</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-a-multilinestring">Create a MultiLineString</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-a-multipolygon">Create a MultiPolygon</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-a-geometrycollection">Create a GeometryCollection</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-geometry-from-wkt">Create Geometry from WKT</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-geometry-from-geojson">Create Geometry from GeoJSON</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-geometry-from-gml">Create Geometry from GML</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#create-geometry-from-wkb">Create Geometry from WKB</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#count-points-in-a-geometry">Count Points in a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#count-geometries-in-a-geometry">Count Geometries in a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#iterate-over-geometries-in-a-geometry">Iterate over Geometries in a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#iterate-over-points-in-a-geometry">Iterate over Points in a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#buffer-a-geometry">Buffer a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#calculate-envelope-of-a-geometry">Calculate Envelope of a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#calculate-the-area-of-a-geometry">Calculate the Area of a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#calculate-the-length-of-a-geometry">Calculate the Length of a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#get-the-geometry-type-as-a-string-from-a-geometry">Get the geometry type (as a string) from a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#calculate-intersection-between-two-geometries">Calculate intersection between two Geometries</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#calculate-union-between-two-geometries">Calculate union between two Geometries</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#write-geometry-to-geojson">Write Geometry to GeoJSON</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#write-geometry-to-wkt">Write Geometry to WKT</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#write-geometry-to-kml">Write Geometry to KML</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#write-geometry-to-wkb">Write Geometry to WKB</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#force-polygon-to-multipolygon">Force polygon to multipolygon</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometry.html#quarter-polygon-and-create-centroids">Quarter polygon and create centroids</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="vector_layers.html">Vector Layers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#delete-a-file">Delete a file</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#is-ogr-installed">Is Ogr Installed</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#view-auto-generated-ogr-help">View Auto Generated Ogr Help</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-list-of-ogr-drivers-alphabetically-a-z">Get List of Ogr Drivers Alphabetically (A- Z)</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#is-ogr-driver-available-by-driver-name">Is Ogr Driver Available by Driver Name</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#force-ogr-use-named-driver">Force Ogr Use Named Driver</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-shapefile-feature-count">Get Shapefile Feature Count</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-all-postgis-layers-in-a-postgresql-database">Get All PostGIS layers in a PostgreSQL Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-postgis-layer-feature-count-by-layer-name">Get PostGIS Layer Feature Count By Layer Name</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-all-layers-in-an-esri-file-geodatabase">Get all layers in an Esri File GeoDataBase</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#load-data-to-memory">Load data to memory</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#iterate-over-features">Iterate over Features</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-geometry-from-each-feature-in-a-layer">Get Geometry from each Feature in a Layer</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#filter-by-attribute">Filter by attribute</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#spatial-filter">Spatial Filter</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-shapefile-fields-get-the-user-defined-fields">Get Shapefile Fields - Get the user defined fields</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-shapefile-fields-and-types-get-the-user-defined-fields">Get Shapefile Fields and Types - Get the user defined fields</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-postgis-layer-fields-get-the-user-defined-fields">Get PostGIS Layer Fields - Get the user defined fields</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-postgis-layer-fields-and-types-get-the-user-defined-fields">Get PostGIS Layer Fields and Types - Get the user defined fields</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-a-layer-s-capabilities">Get a Layer&#8217;s Capabilities</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-wfs-layers-and-iterate-over-features">Get WFS layers and iterate over features</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#set-http-proxy-options-before-fetching-a-web-datasource">Set HTTP Proxy options before fetching a web datasource</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#read-a-csv-of-coordinates-as-an-ogrvrtlayer">Read a CSV of Coordinates as an OGRVRTLayer</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#create-a-new-layer-from-the-extent-of-an-existing-layer">Create a new Layer from the extent of an existing Layer</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#save-the-convex-hull-of-all-geometry-from-an-input-layer-to-an-output-layer">Save the convex hull of all geometry from an input Layer to an output Layer</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#save-centroids-of-input-layer-to-an-output-layer">Save centroids of input Layer to an output Layer</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#create-a-new-shapefile-and-add-data">Create a New Shapefile and Add Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#create-a-postgis-table-from-wkt">Create a PostGIS table from WKT</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#filter-and-select-input-shapefile-to-new-output-shapefile-like-ogr2ogr-cli">Filter and Select Input Shapefile to New Output Shapefile Like ogr2ogr CLI</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#merge-ogr-layers">Merge OGR Layers</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#get-a-list-of-the-street-names-in-a-osm-file">Get a list of the street names in a OSM file</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#create-fishnet-grid">Create fishnet grid</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#convert-polygon-shapefile-to-line-shapefile">Convert polygon shapefile to line shapefile</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#create-point-shapefile-with-attribute-data">Create point shapefile with attribute data</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#create-buffer">Create buffer</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#convert-vector-layer-to-array">Convert vector layer to array</a></li>
<li class="toctree-l2"><a class="reference internal" href="vector_layers.html#convert-polygon-to-points">Convert polygon to points</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="raster_layers.html">Raster Layers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#close-a-raster-dataset">Close a raster dataset</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#get-raster-metadata">Get Raster Metadata</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#get-raster-band">Get Raster Band</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#loop-through-all-raster-bands">Loop Through All Raster Bands</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#get-raster-band-information">Get Raster Band Information</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#polygonize-a-raster-band">Polygonize a Raster Band</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#convert-an-ogr-file-to-a-raster">Convert an OGR File to a Raster</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#clip-a-geotiff-with-shapefile">Clip a GeoTiff with Shapefile</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#calculate-zonal-statistics">Calculate zonal statistics</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#raster-to-vector-line">Raster to vector line</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#create-raster-from-array">Create raster from array</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#create-least-cost-path">Create least cost path</a></li>
<li class="toctree-l2"><a class="reference internal" href="raster_layers.html#replace-no-data-value-of-raster-with-new-value">Replace No Data Value of Raster with new value</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="projection.html">Projection</a><ul>
<li class="toctree-l2"><a class="reference internal" href="projection.html#create-projection">Create Projection</a></li>
<li class="toctree-l2"><a class="reference internal" href="projection.html#reproject-a-geometry">Reproject a Geometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="projection.html#get-projection">Get Projection</a></li>
<li class="toctree-l2"><a class="reference internal" href="projection.html#reproject-a-layer">Reproject a Layer</a></li>
<li class="toctree-l2"><a class="reference internal" href="projection.html#export-projection">Export Projection</a></li>
<li class="toctree-l2"><a class="reference internal" href="projection.html#create-an-esri-prj-file">Create an ESRI.prj file</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="gotchas.html">API Tricks and Trapdoors</a><ul>
<li class="toctree-l2"><a class="reference internal" href="gotchas.html#filtered-features-are-only-respected-using-getnextfeature">Filtered Features Are Only Respected Using GetNextFeature()</a></li>
<li class="toctree-l2"><a class="reference internal" href="gotchas.html#features-and-geometries-have-a-relationship-you-don-t-want-to-break">Features and Geometries Have a Relationship You Don&#8217;t Want to Break</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="indices-and-tables">
<h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">¶</a></h1>
<ul class="simple">
<li><a class="reference internal" href="genindex.html"><span>Index</span></a></li>
<li><a class="reference internal" href="py-modindex.html"><span>Module Index</span></a></li>
<li><a class="reference internal" href="search.html"><span>Search Page</span></a></li>
</ul>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="#">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Welcome to the Python GDAL/OGR Cookbook!</a></li>
<li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
</ul>

  <h4>Next topic</h4>
  <p class="topless"><a href="gdal_general.html"
                        title="next chapter">GDAL/OGR General</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/index.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="gdal_general.html" title="GDAL/OGR General"
             >next</a> |</li>
        <li class="nav-item nav-item-0"><a href="#">Python GDAL/OGR Cookbook 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2013, Jared Erickson, Cort Daniel, Michael Payne.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
    </div>
  </body>
</html>